package com.log.fenxi;

import com.google.common.base.Charsets;
import com.google.common.base.Strings;
import com.google.common.io.FileWriteMode;
import com.google.common.io.Files;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author carter
 * create time    2018-07-23
 */
public class LogToSql {



    public static void main(String[] args) {

        String dayFile = "20180719";

        List<String> lineList = readFileToList(dayFile);

        System.out.println("读取的文件总行数："+ lineList.size());

        List<LogRecord> logRecordList = transLogRecordList(lineList);

        System.out.println("转换过滤之后的总行数："+ logRecordList.size());

        List<List<LogRecord>> taskList = ListUtils.partition(logRecordList,10000);

        File sqlFile =  geneSqlFile(dayFile);

        taskList.stream().forEach(task->geneTaskSql(task,sqlFile));



    }

    private static File geneSqlFile(String dayFile) {

        LocalDate fileDayLocalDate = LocalDate.parse(dayFile, DateTimeFormatter.ofPattern("yyyyMMdd"));
        String deleteStrin  = "delete from t_access_time where day='"+fileDayLocalDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))+"';";

        File to = new File("d:/log_fenxi/insert" + dayFile+".sql");
        try {
            if (to.exists())
            {
                to.delete();
            }

            to.createNewFile();

            Files.asCharSink(to, Charset.forName("gbk"), FileWriteMode.APPEND).write(deleteStrin);

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("创建生成的SQL文件失败");
        }

        return to;
    }

    private static List<LogRecord> transLogRecordList(List<String> lineList) {
       return lineList.stream().map(line -> {

            String[] split = StringUtils.split(line, " ");
            String timeString = split[4].substring(1);
            LocalDateTime dateTime = LocalDateTime.parse(timeString, DateTimeFormatter.ofPattern("dd/MMM/uuuu:HH:mm:ss", Locale.US));
            String url = split[7];
            if (url.contains("?"))
            {
                url = url.substring(0,url.indexOf("?"));
            }
            if (url.startsWith("/admin/getUserByTicket/"))
            {
                url = url.substring(0,"/admin/getUserByTicket".length());
            }
            if (url.startsWith("/tms/shippingParcel/onceAgainAssignShipping"))
            {
                url = url.substring(0,"/tms/shippingParcel/onceAgainAssignShipping".length());
            }
            if (url.startsWith("/tms/shippingParcel/onceAgainAssignShipping"))
            {
                url = url.substring(0,"/tms/shippingParcel/onceAgainAssignShipping".length());
            }

            long request_time = getRequest_time(line, split);

            String day = dateTime.toLocalDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            String add_time = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            String module = getModule(url);
            String end_fix = getEndFix(url);

            return new LogRecord(day, add_time, url, request_time, module, end_fix);

        }).filter(item ->
                !Strings.isNullOrEmpty(item.getUrl()) &&
                !Strings.isNullOrEmpty(item.getModule())&&
                !Strings.isNullOrEmpty(item.getEnd_fix())

       )

               .collect(Collectors.toList());
    }

    private static List<String> readFileToList(String dayFile) {

        String fileName = "d:/"+dayFile+".txt";

        try {
            File file = new File(fileName);
            System.out.println("读取的文件是："+fileName);
            return Files.readLines(file, Charsets.UTF_8);
        } catch (IOException e) {
            System.out.println("读取文件失败，fileName:" + fileName);
            e.printStackTrace();
        }

        return null;
    }

    private static void geneTaskSql(List<LogRecord> task, File sqlFile) {

        StringBuffer stringBuffer = new StringBuffer("\nINSERT INTO t_access_time(day, add_time, url, request_time, module, end_fix) VALUES ");


        task.stream().forEach(item->{

            stringBuffer.append("('").append(item.getDay()).append("','");
            stringBuffer.append(item.getAdd_time()).append("','");
            stringBuffer.append(item.getUrl()).append("',");
            stringBuffer.append(item.getRequest_time()).append(",'");
            stringBuffer.append(item.getModule()).append("','");
            stringBuffer.append(item.getEnd_fix()).append("'),");
        });

        stringBuffer.deleteCharAt(stringBuffer.length()-1);
        stringBuffer.append(";");

        try {
            Files.asCharSink(sqlFile, Charset.forName("gbk"), FileWriteMode.APPEND).write(stringBuffer.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }



    public static long getRequest_time(String line, String[] split) {
        long request_time = 0;
        try {
            String timeCostString = split[split.length - 3];
            if (timeCostString.endsWith(","))
            {
                timeCostString = timeCostString.substring(0,timeCostString.length()-1);
            }
            request_time = new BigDecimal(timeCostString).multiply(new BigDecimal(1000)).longValue();
        } catch (NumberFormatException e) {
            System.out.println("出错行信息："+ line );
            request_time = 0;
        }


        return request_time;
    }

    public static String getEndFix(String url) {
        if (Strings.isNullOrEmpty(url))  {
            return "";
        }

        if (!url.contains("."))
        {
            return "java";
        }

        return url.substring(url.lastIndexOf(".")+1);

    }

    public static String getModule(String url) {
        if (Strings.isNullOrEmpty(url))  {
            return "";
        }

        if (url.startsWith("/"))
        {
            String[] split = StringUtils.split(url, "/");
            if (Objects.isNull(split) ||  split.length<1)
            {
                return url;
            }
            String module = split[0];
            return  module;
        }else{
            System.out.println("非标准的URL:"+url);
        }
        return "";

    }

}
